﻿using System;

namespace _2.clock
{
    
        public delegate void timeChange(object sender, TimeEventArgs e);

        public class TimeEventArgs
        {
            public TimeEventArgs()
            {
                timeStep();
            }

            public void timeStep()
            {
                this.hour = DateTime.Now.Hour.ToString();
                this.minute = DateTime.Now.Minute.ToString();
                this.second = DateTime.Now.Second.ToString();
            }

            public string hour { get; set; }
            public string minute { get; set; }
            public string second { get; set; }
        }

    

    class Clock
    {
        //Event timeEvent = new Event();
        public event timeChange OnTimeChange;
        TimeEventArgs args;
        public Clock()
        {
            isSetAlarm = false;
            args = new TimeEventArgs();
            OnTimeChange += new timeChange(Tick);
            OnTimeChange += Alarm;
            OnTimeChange(this, args);
        }

        public void timeStep()
        {
            DateTime now = DateTime.Now;
            if (now.Hour.ToString() != args.hour || now.Minute.ToString() != args.minute || now.Second.ToString() != args.second)
            {
                args.timeStep();
                OnTimeChange(this, args);
            }
        }

        public void Tick(object sender, TimeEventArgs e)
        {
            Console.WriteLine("嘀嗒，当前时间为：{0}:{1}:{2}", e.hour, e.minute, e.second);
        }

        public string alarmHour { get;set; }
        public string alarmMinute { get;set; }
        public string alarmSecond { get;set; }
        public bool isSetAlarm { get; set; }

        public void setAlarmClock(string hour,string minute)
        {
            this.alarmHour = hour;
            this.alarmMinute = minute;
            //this.alarmSecond = second;
        }

        public void Alarm(object sender, TimeEventArgs e)
        {
            if (e.hour == this.alarmHour && e.minute == this.alarmMinute)
            {
                Console.WriteLine("闹钟响了，丁铃铃铃................");
            }
        }
    }

    class Program
    {
        static bool isDigit(string num)
        {
            for (int i = 0; i < num.Length; ++i)
            {
                if (num[i] < '0' || num[i] > '9') return false;
            }
            return true;
        }

        static bool isInRange(string num,int left,int right)
        {
            if (!isDigit(num)) return false;
            int n = Convert.ToInt32(num);
            if (left <= n && n <= right) return true;
            else return false;
        }

        static void Main(string[] args)
        {
            Clock clock = new Clock();
            Console.WriteLine("您是否要设置闹钟，请回复“是”或“否”：");
            string isAlarm = Console.ReadLine();
            while (isAlarm != "是" && isAlarm != "否")
            {
                Console.WriteLine("输入有误");
                Console.WriteLine("请回复“是”或“否”：");
                isAlarm = Console.ReadLine();
            }

            if (isAlarm == "是")
            {
                Console.WriteLine("请输入闹钟小时的时间：");
                string hour = Console.ReadLine();
                while (!isInRange(hour, 0, 23))
                {
                    Console.WriteLine("输入有误, 请输入闹钟小时的时间：");
                    hour = Console.ReadLine();
                }

                Console.WriteLine("请输入闹钟分钟的时间：");
                string minute = Console.ReadLine();
                while (!isInRange(minute, 0, 59))
                {
                    Console.WriteLine("输入有误, 请输入闹钟分钟的时间：");
                    minute = Console.ReadLine();
                }
                clock.setAlarmClock(hour, minute);
                Console.WriteLine("您的闹钟设置成功，将在{0:02}:{1:02}为您响铃", hour, minute);
            }

            while (true) clock.timeStep();
        }
    }
}
